package cn.admin.configcenter.configservice.service;

import cn.admin.configcenter.configservice.domain.ConfigInfo;
import cn.admin.configcenter.configservice.domain.Page;
import cn.admin.configcenter.configservice.persist.ConfigAdvancedDAO;
import cn.admin.configcenter.configservice.util.ConfigKey;
import cn.admin.configcenter.configservice.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ConfigSDKService {

	private static Logger LOG = LoggerFactory.getLogger(ConfigSDKService.class);

	@Autowired
	private ConfigAdvancedDAO configAdvancedDAO;

	@Autowired
	private ConfigService configService;

	@Autowired
    private LongPullingService longPullingService;

	public Page<ConfigInfo> findConfigInfo(int pageNo,int pageSize,String group,String dataId) {
		try {
			if (!StringUtils.isBlank(dataId) && !StringUtils.isBlank(group)) {
				return configAdvancedDAO.findConfigInfoByDataIdAndGroup(pageNo,pageSize,dataId,group);
			} else if (!StringUtils.isBlank(dataId)) {
				return configAdvancedDAO.findConfigInfoByDataId(pageNo,pageSize,dataId);
			} else {
				return !StringUtils.isBlank(group) ?
						this.configAdvancedDAO.findConfigInfoByGroup(pageNo, pageSize, group) :
						this.configAdvancedDAO.findAllConfigInfo(pageNo, pageSize);
			}
		} catch (Exception e) {
			String errorMsg = "query page config info error, dataId=" + dataId + ", group=" + group +
					", pageNo=" + pageNo + ", pageSize=" + pageSize;
			LOG.error(errorMsg, e);
			throw new ConfigServiceServerException(e);
		}
	}

	public boolean createOrUpdateConfig(String dataId,String group,String content,String srcIp,
                                        String appName) {
	    return this.configService.createOrUpdateConfig(dataId,group,content,srcIp,appName);
    }

	public Page<ConfigInfo> listConfigLike(int pageNo,int pageSize,String group,String dataId,
	                                       String content,String srcIp) {
		return configAdvancedDAO.findConfigInfoLike(pageNo,pageSize,group,
				dataId,content,srcIp);
	}

	public void removeConfig(String dataId,String group,String srcIp) {
		this.configService.removeConfig(dataId,group,srcIp);
	}

    public Page<ConfigInfo> listPublishedConfigByIp(int pageNo, int pageSize, String clientIp) {
        return this.configAdvancedDAO.findPublishedConfigInfoByIp(pageNo, pageSize, clientIp);
    }

    public Page<ConfigInfo> listSubscribedConfigByIp(int pageNo, int pageSize, String clientIp) {
        List<ConfigInfo> result = new ArrayList<>();
        Page<ConfigInfo> page = new Page<>();
        List<String> subscribedConfigKeys = this.longPullingService.getSubscribedConfigKeyByClientIp(clientIp);

        for (String configKey : subscribedConfigKeys) {
            String[] dataIdAndGroup = ConfigKey.parseKey(configKey);
            result.add(new ConfigInfo(dataIdAndGroup[0], dataIdAndGroup[1]));
        }

        page.setCurrentPage(pageNo);
        page.setPageSize(pageSize);
        page.setTotalSize(subscribedConfigKeys.size());
        page.setResult(result);
        return page;
    }

}
